program mynormal5, sortpreserve
	version 8.1
	if replay() {
		if (`"`e(cmd)'"' != "mynormal5") error 301
		Replay `0'
	}
	else	Estimate `0'
end

program Estimate, eclass
	syntax varlist [if] [in]		///
		[fweight pweight] [,		/// <- NEW
		noLOg				/// -ml model- options
		noCONStant			///
		Robust				///
		CLuster(varname)		///
		HETero(varlist)			///
		Level(integer `c(level)')	/// -Replay- option
		*				/// -mlopts- options
	]

	// check syntax
	mlopts mlopts, `options'
	gettoken lhs rhs : varlist
	if "`cluster'" != "" {
		local clopt cluster(`cluster')
	}
	if "`weight'" != "" {				// NEW block
		tempvar wvar
		quietly gen double `wvar' `exp'
		local wgt "[`weight'=`wvar']"
	}
	// mark the estimation sample
	marksample touse
	markout `touse' `hetero' `wvar'			// <- CHANGED
	markout `touse' `cluster', strok

	// fit the full model
	ml model d2 mynormal_d2				///
		(mu: `lhs' = `rhs', `constant')		///
		(lnsigma: `hetero')			///
		`wgt' if `touse',			/// <- CHANGED
		`log'					///
		`mlopts'				///
		`robust'				///
		`clopt'					///
		missing					///
		maximize

	ereturn local cmd mynormal5

	Replay , level(`level')
end

program Replay
	syntax [, Level(integer `c(level)') ]
	ml display , level(`level')
end
